package com.prognos.service.harvester.impl;

import com.google.common.collect.Lists;
import com.prognos.service.harvester.KeywordExtractor;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by chema on 21/12/14.
 */
@Service("keywordExtractor")
public class KeywordExtractorImpl implements KeywordExtractor {

    @Override
    public List<String> fromHeadSection(Document webDocument) {
        List<String> keywords = Lists.newLinkedList();
        Elements metaElements = webDocument.select("meta[property=article:tag");
        if(metaElements != null) {
            for (Element meta : metaElements) {
                String content = meta.attr("content");
                if (!StringUtils.isEmpty(content)) {
                    keywords.add(content.toLowerCase().replace(' ','_'));
                }
            }
        }
        return keywords;
    }
}
